# -*- coding: utf-8 -*-
# @Time    : 2020/8/21 13:42
# @Author  : Leo
# @FileName: wechat_email_handler.py
# @Software: PyCharm

import tornado.web
from modules.user.user_manage import UserManage
from arpylibs.basehandler.handler_result import deco_deal_error
from modules.user.wechat.wechat_request import WechatRequest
from utils.error.ar_exception import ARUserException,USER_STATUS_POOL

class WechatEmailHandler(tornado.web.RequestHandler):
    """
    """
    @deco_deal_error
    def post(self, user_id):
        """
        发送微信绑定邮件
        """
        UserManage().send_add_wechat_email(user_id)

    @deco_deal_error
    def put(self, user_id):
        """
        更换微信绑定邮件
        """
        UserManage().send_change_add_wechat_email(user_id)

    @deco_deal_error
    def delete(self, user_id):
        """
        解除微信绑定  并向服务号通知消息
        """
        # 检查用户是否存在
        if not  UserManage().exists(user_id):
            raise ARUserException(USER_STATUS_POOL.ID_USER_NOT_EXISTS)
        # 检查用户是否绑定微信
        if not UserManage().is_wechatId_exists(userId=user_id):
            raise ARUserException(USER_STATUS_POOL.ID_USER_WECHAT_EMPTY)
        loginName, wechatId = UserManage().delete_user_wechat_by_userId(userId=user_id)
        # 发送解绑消息
        WechatRequest.sendMessage(openid=wechatId,
                                  content=WechatRequest.ByeByeMessage.replace('test_user', loginName))